package com.burns.rbac.commons;

import lombok.Data;

import java.util.HashMap;

@Data
public class R  extends HashMap {
    public static final String CODE="code";
    public static final String ErrorCode="errorCode";
    public static final String MSG="msg";
    public static final String DATA="data";
//    /**
//     * 响应的状态码
//     */
//    private Integer code;
//
//    /**
//     * 状态信息
//     */
//    private String msg;
//
//    /**
//     * 数据
//     */
//    private Object data;

    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key   key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     * <tt>null</tt> if there was no mapping for <tt>key</tt>.
     * (A <tt>null</tt> return can also indicate that the map
     * previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public R put(String key, Object value) {
        super.put(key,value);
        return this;
    }

    public static R ok(){
        R r = new R();
        r.put(CODE,200);//200代表成功
        return r;
    }

    public static R ok(Object data){
        R success = ok();
        success.put(DATA,data);
        return success;
    }

    public static R error(){
        R r = new R();
        r.put(CODE,500);
        return r;
    }
    public static R error(String msg){
        R error = error();
        error.put(MSG,msg);
        return error;
    }
    public static R error(String errorCode,String msg){
        R error = error();
        error.put(MSG,msg);
        error.put(ErrorCode,errorCode);
        return error;
    }

    public static R write(boolean flag) {
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
